$(function () {
    var form = layui.form
    var laypage = layui.laypage
    // 补零函数
    function addZero(n) {
        return n < 10 ? "0" + n : n
    }
    // 处理时间的函数
    template.defaults.imports.getTime = function (T) {
        var time = new Date(T);
        var year = time.getFullYear();
        var month = addZero(time.getMonth() + 1)
        var date = addZero(time.getDate())
        var h = addZero(time.getHours())
        var m = addZero(time.getHours())
        var s = addZero(time.getSeconds())
        return year + "-" + month + "-" + date + ' ' + h + ":" + m + ":" + s
    }

    var data = {
        pagenum: 1,
        pagesize: 2,
        cate_id: "",
        state: ""
    }
    getArt()

    // TODO:渲染页面的函数
    function getArt() {
        $.ajax({
            url: "/my/article/list",
            data: data,
            success: function (res) {
                // console.log(res)
                var str = template("tpl_art", res)
                $("tbody").html(str)
                page(res.total)
            }
        })
    }

    // TODO:封装分页的函数
    function page(n) {
        laypage.render({
            elem: 'page', //注意，这里的 test1 是 ID，不用加 # 号
            count: n, //数据总数，从服务端得到
            layout: ['prev', 'page', 'next', 'limit'],
            limit: 2,
            limits: [2, 4, 6, 8, 10],  // 一页显示多少条数据
            groups: 3,
            curr: data.pagenum,
            jump: function (obj, first) {
                //首次不执行
                if (!first) {
                    //do something
                    console.log(obj)
                    data.pagesize = obj.limit;
                    data.pagenum = obj.curr;
                    getArt()
                }
            }
        });
    }





    // TODO:删除函数
    $("body").on("click", "#delArt", function () {
        var that = $(this)
        layer.confirm('确定删除?', { icon: 3, title: '提示' }, function (index) {
            //do something
            var id = that.attr("data-id")
            $.ajax({
                url: "/my/article/delete/" + id,
                success: function (res) {
                    if (res.status != 0) {
                        return layer.msg(res.message)
                    }
                    layer.msg(res.message)
                    getArt()
                }
            })
            layer.close(index);

        })
    })

    // TODO:下拉框
    getSel();
    function getSel() {
        $.ajax({
            url: "/my/article/cates",
            success: function (res) {
                var strSel = template("tpl_select", res)
                $("#selectBox").html(strSel)
                // 更新渲染
                form.render("select")
            }
        })
    }

    //TODO:表单提交渲染数据
    $("form").on("submit", function (e) {
        e.preventDefault();
        // 收集表单数据
        var data1 = form.val("fr");
        data.cate_id = data1.cate_id;
        data.state = data1.state
        getArt()
    })


    $("body").on("click", ".editCatas", function () {
        var id = $(this).attr("data-id")
        location.href = "/article/art-edit.html?id=" + id;
    })

})
